const web = require("../../module/web"), loger = require("../../module/loger");
const nA = ["小","好","二","大","好看的","美丽的","轻轻的","火的","你的","我的","好的","明的","暗的","新的","高的","低的","爱的","旧","铁","黑的","白的","哭的","丑","灰","坏","烫","凉","冷","热","温","傻","看","走","清","请","问","闻","吃","喝","电","红","绿","黄","蓝","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","一","二","三","四","五","六","七","八","九","十"];
const nB = ["桌子","电脑","人","狗","猫","太阳","月亮","水果","苹果","房子","自行车","手机","钱包","铅笔","被子","杯子","水壶","电视","窗","门","梦","拖把","鸭","鹅","兴趣","心情","情怀","白茶","茶叶","好笑","好心","神话","生活","成功","理想","衣服","时间","机器","水平","能力","生命","神秘","黑板","糖","蜜","开关","核桃","桃子","香蕉","灯","盘","水","磁铁","垃圾","桶","饺子","勺子","药水"];
let cgn = 0, cgt = 0;

module.exports.run = (yzm)=>{
	web.run((w)=>{
		cgn = cgt = 0
		let uf = w.window.document.getElementById("userinfo");
		if(uf){
			let al = uf.getElementsByTagName("a");
			
			for(let i=0;i<al.length;i++){
				if(al[i].href.indexOf("/i.360.cn/") != -1){
					al[i].click();
					break;
				}
			}		
		}
	}, (w)=>{ //页面重新加载后
		loger.info("info", w.window.location.href);
		if(w.window.location.href.indexOf("/i.360.cn/profile/chnickname") != -1){ //修改昵称页面
			let tp = w.window.document.getElementsByClassName("quc-tip-error");
			if(tp && tp[0] && tp[0].clientWidth > 0){
				loger.info("input nn", tp[0].clientWidth);
				let mm = w.window.document.getElementsByClassName("quc-link");
				for(let i=0;i<mm.length;i++){
					mm[i].click();
					let inp = w.window.document.getElementsByClassName("quc-submit quc-button quc-button-save");
					if(inp && inp[0]) {
						inp[0].click();
						cgn = 2;
					}
					break;
				}
				return;
			}
			loger.info("input nickname");
			let inp = w.window.document.getElementsByName("nickname");
			if(inp && inp[0]){
				//inp[0].value = nA[parseInt(Math.random()*nA.length)] + nB[parseInt(Math.random()*nB.length)];
				inp[0].value = global.sdObj.ud[13].nn;
			}
			inp = w.window.document.getElementsByClassName("quc-submit quc-button quc-button-save");
			if(inp && inp[0]) {
				inp[0].click();
				cgn = 2;
			}
		}else if(w.window.location.href.indexOf("/i.360.cn/profile/avatar") != -1){ //修改头像
			let mg = w.window.document.getElementById("errmsg");
			if(mg && mg.innerHTML.indexOf("操作成功") > 0){
				w.window.location.href = "http://i.360.cn/";
				return;
			}
			let al = w.window.document.getElementsByClassName("lnk");
			for(let i=0;i<al.length;i++){
				if(al[i].innerHTML == "普通上传模式"){
					al[i].click();
					break;
				}
			}
		}else if(w.window.location.href.indexOf("/i.360.cn/") != -1){
			if(cgn == 0){
				let al = w.window.document.getElementsByTagName("a");
				for(let i=0;i<al.length;i++){
					if(al[i].innerHTML == "设置昵称"){
						al[i].click();
						cgn = 1;
						return;
					}else if(al[i].innerHTML == "修改昵称"){
						w.window.location.href = "http://zhushou.360.cn/";
						cgn = 2;
						//return true;
					}
				}				
			}
			if(cgn == 2 && cgt == 0){
				let al = w.window.document.getElementsByClassName("avatar-img");
				if(al && al[0]){
					al = al[0].getElementsByTagName("img");
					if(al && al[0] && al[0].src.indexOf("t00df551a583a87f4e9") == -1) cgt = 1;
				}
				if(cgt == 0){
					al = w.window.document.getElementsByClassName("change");
					for(let i=0;i<al.length;i++){
						if(al[i].innerHTML == "修改头像"){
							al[i].click();
							cgt = 1;
							return;
						}
					}					
				}
			}
			if(cgn == 2 && cgt == 1){
				global.sdObj.end = 0;
				return true;
			}
		}
	})
}